(function($){
	
	
	$.extend($.fn.tabs.methods, {
	    getTabById: function(jq,id) {
	        var tabs = $.data(jq[0], 'tabs').tabs;
	        for(var i=0; i<tabs.length; i++){
	            var tab = tabs[i];
	            if (tab.panel('options').id == id){
	                return tab;
	            }
	        }
	        return null;
	    },
	    selectById:function(jq,id) {
	        return jq.each(function() {
	            var state = $.data(this, 'tabs');
	            var opts = state.options;
	            var tabs = state.tabs;
	            var selectHis = state.selectHis;
	            if (tabs.length == 0) {return;}
	            var panel = $(this).tabs('getTabById',id); // get the panel to be activated 
	            if (!panel){return}
	            var selected = $(this).tabs('getSelected');
	            if (selected){
	                if (panel[0] == selected[0]){return}
	                $(this).tabs('unselect',$(this).tabs('getTabIndex',selected));
	                if (!selected.panel('options').closed){return}
	            }
	            panel.panel('open');
	            var title = panel.panel('options').title;        // the panel title 
	            selectHis.push(title);        // push select history 
	            var tab = panel.panel('options').tab;        // get the tab object 
	            tab.addClass('tabs-selected');
	            // scroll the tab to center position if required. 
	            var wrap = $(this).find('>div.tabs-header>div.tabs-wrap');
	            var left = tab.position().left;
	            var right = left + tab.outerWidth();
	            if (left < 0 || right > wrap.width()){
	                var deltaX = left - (wrap.width()-tab.width()) / 2;
	                $(this).tabs('scrollBy', deltaX);
	            } else {
	                $(this).tabs('scrollBy', 0);
	            }
	            $(this).tabs('resize');
	            opts.onSelect.call(this, title, $(this).tabs('getTabIndex',panel));
	        });
	    },
	    existsById:function(jq,id){
	        return $(jq[0]).tabs('getTabById',id) != null;
	    }
	});
	
	
	
	
})(jQuery);
(function($){
	 $.fn.cmenu = function (options, param){
		 $.each(this,function(index,ele){
			 var opts = $.extend({}, $.fn.cmenu.defaults, $.fn.cmenu.parseOptions(ele), options);
			 $(this).tree({
				 onClick : function(node) {
					 var tt = node.text;
					 var tab = $("#operating-space").tabs("getTabById",opts.id);
					 if(tab==null){
						 $("#operating-space").tabs('add',{id:opts.id,title:$(tt).text(),href:$(tt).attr("url"),closable:true,border:false,fit:true});
					 }else{
						 $("#operating-space").tabs('update',{tab:tab,options:{id:opts.id,title:$(tt).text(),href:$(tt).attr("url"),closable:true,border:false,fit:true}});
						 tab.panel('refresh');
					 }
					 
				 }
			 
			 });
		 });
		 
		 
	 };
	 
	 $.fn.cmenu.defaults = {
			    id: 'cooge'
			};
	 $.fn.cmenu.parseOptions = function(target) {
		    return $.extend({},$.parser.parseOptions(target,["id"]));
		};
	 
	 
	$.parser.plugins.push("cmenu");
})(jQuery);

(function($){

	
	var _2 = function($pal){
		
		if($pal==null){
			return;
		}
		/**
		 * 右边菜单
		 */
		$(".ul-menu,.ul-submenu",$pal).each(function(){
			var $this=$(this);
			$(">li",$this).each(function(){
				var $li=$(this);
				var $a=$(">a",$li);
				var $sub=$li.find(">.ul-submenu");
				if($sub.length==1){
					var $i=$("<i class='boot_icon-chevron-right f-right'></i>").appendTo($a);
					$a.css({"background-color":"#E0ECFF","color":"black"});
					$a.click(function(){
						if($sub.css("display")=="none"){
							$sub.show();
							$i.removeClass("boot_icon-chevron-right");
							$i.addClass("boot_icon-chevron-down");
							
						}else{
							$sub.hide();
							$i.removeClass("boot_icon-chevron-down");
							$i.addClass("boot_icon-chevron-right");
						}
					});
				}
			});
			
		});
		
		/**
		 * 标签 a 的控制
		 */
		
		$("a",$pal).each(function(){
			$(this).click(function(){
				var node = $(this);
				var target = $(this).attr("target");
				var rel = $(this).attr("rel");
				rel =  (rel==null||rel=="null"?"cooge":rel);
				var href = node.attr("href");
				if(href=="null"||href==null||href.length<2){
					return false;
				}
				
				if(href.indexOf("rel=")==-1){
						if(href.indexOf("?")==-1){
							href =href+"?rel="+rel;
						}else{
							href =href+"&rel="+rel;
						}
						
				}
				
				
				var title = node.attr("title");
				
				var dg = $("#dg_"+rel);
				if(dg.length>0){
					/**
					 * 判断是否是带参数
					 */
					var pamas = href.match(/{.+\}/g);
					if(pamas!=null&&pamas.length>0){
						var row = dg.datagrid("getSelected");
						if(row==null){
							var warn = node.attr("warn");
							$.messager.alert("提示",warn==null?"请选择一个":warn);
							return false;
						}
						$.each(pamas,function(index,ele){
							var text = ele.substring(1);
							text = text.substring(0,text.length-1);
							href = href.replace("{"+text+"}",row[text]);
						});
					}
				}
				
				/**
				 * 标签的方式打开页面
				 */
				if(target=="navTab"){
					 var tab = $("#operating-space").tabs("getTabById",rel);
					 if(tab==null){
						 $("#operating-space").tabs('add',{id:rel,title:node.text(),href:href,closable:true,border:false,fit:true});
					 }else{
						 if(node.text()!=tab.title){
							 $("#operating-space").tabs('update',{tab:tab,options:{id:rel,title:node.text(),href:href,closable:true,border:false,fit:true}});
							 tab.panel('refresh');
						 }
						 
					 }
					 var index = $('#operating-space').tabs('getTabIndex',tab);
					 $("#operating-space").tabs('select',index);
				}
				/**
				 * 弹窗的方式打开页面
				 */
				
				if(target=="dialog"){
					var drel = "dialog_"+rel;
					var dd = $("#"+drel);
					if(dd.length==0){
						dd = $("<div id=\""+drel+"\"></div>");
					};
					
					var height = node.attr("height");
					var width = node.attr("width");
					
					dd.dialog({
					    title: title,
					    padding: 20,
					    width: width==null?400:width,
					    height:height==null?400:height,
					    closed: false,
					    cache: false,
					    href: href
					});
				}
				
				
				/**
				 * 以panel的方式打开页面
				 */
				
				
				if(target=="panel"){
					$("#panel_"+rel).panel('open').panel('refresh',href);
					
				}
				/**
				 * 删除操作,
				 */
				
				if(target=="ajaxTodo"){
					
					$.messager.confirm('提示', '确认继续?', function(r){
						if(r){
							$.messager.progress({ title:'提示',msg:'正在操作...'});
							$.ajax({
								url:href,success:function(result){
									$.messager.progress('close');
									$.parser.parse($(result));
								},error:function(){
									$.messager.progress('close');
									$.messager.alert("提示","提交出错","error");
								}
							});
						}
						
						
					});
				}
				
				
				
				/**
				 * 不弹窗,只是操作,表格专用
				 * selectedTodo
				 */
				
				if(target=="selectedTodo"){
					
					
					$.messager.confirm('提示', '确认继续?', function(r){
						
						if(r){
							$.messager.progress({ title:'提示',msg:'正在操作...'});
							
							$.ajax({
								
								url:href,success:function(result){
									$.messager.progress('close');
									
									var dg = $("#dg_"+rel);
									dg.datagrid("reload");
									
									$.parser.parse($(result));
									
								},error:function(){
									$.messager.progress('close');
									$.messager.alert("提示","提交出错","error");
								}
							});
						}
					});
					
				}
				
				return false;
			});
		});
		/**
		 * 菜单管理zTree控制
		 */
		
		
		var mtree = $(".menu-tree",$pal);
		if(mtree.length>0){
			mtree.each(function(){
				var this_ = $(this);
				var panelurl = this_.attr("panelurl");
				var rel = this_.attr("rel");
				
				var setting = {
						data : {
							simpleData : {
								enable : true
							}
						},
						callback:{
							
							onClick:function(event, treeId, treeNode, clickFlag){
								var url = panelurl.replace("{id}",treeNode.id);
								$('#panel_'+rel).panel({href:url});
							}
						}
						
				};
				var url = this_.attr("url");
				var data = {};
				$.get(url, data, function(value) {
					var zNodes =jQuery.parseJSON(value);
					$.fn.zTree.init(this_, setting, zNodes);
				});
			});
		}
		/**
		 * 表单控制
		 */
		var mform = $("form[action]",$pal);
		if(mform.length>0){
			
			mform.each(function(){
				
				$(this).submit(function(){
					
					var tf = this;
					
					$.messager.confirm('提示', '确认继续?', function(r){
		                if (r){
							$(tf).ajaxSubmit({
								
								beforeSubmit:function(arr, $form, options){
									$.messager.progress({ title:'提示',msg:'数据提交中...'});
									
								},success:function(responseText, statusText, xhr, $form){
									
									$.messager.progress('close');
									
									var cci   = $form.find("input[name='currentCallback']");
									var did_ = $form.find("input[name='datagridId']");
									if(cci.length>0){
										var cc = cci.val();
										if(cc=="close"){
											var tid = did_.val();
											var did = tid.substring(tid.indexOf("_")+1);
											var drel = "dialog_"+did;
											var dd = $("#"+drel);
											dd.dialog("close");
										}
										
									}
									if(did_.length>0){
										var tid = did_.val();
										$('#'+tid).datagrid("reload");
									}
									$.parser.parse($(responseText));
								},error:function(){
									$.messager.progress('close');
									$.messager.alert("提示","提交出错","error");
									
								}
							});
		                    
		                }
		            });
					return false;
				});
			});
			
		}
		
		/**
		 * 信息反馈
		 * 
		 */
		
		var formresult = $(".form-result",$pal);
		if(formresult.length>0){
			formresult.each(function(){
				var fs = $(this);
				var text = fs.text();
				var jo =jQuery.parseJSON(text);
				$.messager.alert("提示",jo.result,jo.type);
			});
		}
		/**
		 * 富文本编辑
		 */
		
		var editor =$(".kinde_ditor",$pal); 
		if(editor.length>0){
			KindEditor.create(editor,{
                                uploadJson : 'editor/uploadFile.html',
                				fileManagerJson : 'editor/filemanager.html',
                				allowFileManager : true
                
                });
		}
		/**
		 * table 关键词 查询 
		 */
		
		var toolbar =$("div[id^='toolbar_']",$pal);
		
		if(toolbar.length>0){
			toolbar.each(function(){
				var this_=$(this);
				var id = (this_.attr("id"));
				var dg = "dg_"+id.substring(8);
				var from = this_.find("form");
				from.submit(function(){
					$('#'+dg).datagrid('options').queryParams=$.formtoJson(this_.find("form")); 
					$('#'+dg).datagrid('reload');
					return false;
				});
			});
			
		}
	};
	
	$.parser.onComplete=_2;
	
})(jQuery);